package com.wuzuqing.android.eightdiagrams.task;

import com.wuzuqing.android.eightdiagrams.model.QiMenDunJiaModel;
import com.wuzuqing.android.eightdiagrams.term.JieQi;
import com.wuzuqing.android.eightdiagrams.term.Lunar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @CopyRight: 广州仰望星空云科技有限公司
 * @Author : wuzuqing
 * @Email:wuzuqing@xinyu668.com
 * @CreateTime: 26.7.24 3:30 PM
 * @Description: 请写描述
 */
public class QiMenDunJiaStep2Task extends IQiMenDunJiaTask {
    QiMenDunJiaModel model;
    private static String[] yangDun = new String[]{
            "冬至",
            "小寒",
            "大寒",
            "立春",
            "雨水",
            "惊蛰",
            "春分",
            "清明",
            "谷雨",
            "立夏",
            "小满",
            "芒种"
    };
    private String mJu;

    public QiMenDunJiaStep2Task(QiMenDunJiaModel model, String ju) {
        this.model = model;
        this.mJu = ju;
    }

    @Override
    public void handleTask() {
        Set<String> yangDunSet = new HashSet<>(Arrays.asList(yangDun));
        Lunar lunar = model.getLunar();
        JieQi jieQi = model.getLunar().getPrevJieQi();
        String[] Zhi = new String[]{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
        List<String> zhiList = new ArrayList<>(Arrays.asList(Zhi));
        List<String> baZi = lunar.getBaZi();
        int shiShu = zhiList.indexOf(baZi.get(3).charAt(1) + "") + 1;
        model.setShiShu(shiShu);
        int juShu = 0;
        boolean isYangDun = false;
        if ("无".equals(mJu)) {
            isYangDun = yangDunSet.contains(jieQi.name);
            //局数取（年支序数+月数+日数+时支序数）除9之余数为局数。
            int yearShu = zhiList.indexOf(baZi.get(0).charAt(1) + "") + 1;
            juShu = (yearShu + lunar.getMonth() + lunar.getDay() + shiShu) % 9;
//            System.out.println("yearShu=" + yearShu + ",month=" + lunar.getMonth()
//                    + ",day=" + lunar.getDay() + ",shiShu=" + shiShu);
            if (juShu == 0) {
                juShu = 9;
            }
        } else {
            juShu = Integer.parseInt(mJu.charAt(1) + "");
            isYangDun = mJu.contains("阳");
        }
        model.setJuShu(juShu);
        model.setYangDun(isYangDun);
        model.setDingJu((jieQi.name + "-" + lunar.getNextJieQi().name) + " " + (isYangDun ? "阳" :
                "阴") + juShu + "局");
//        System.out.println("第二步:定局=" + model.getDingJu());
    }
}
